
//
//  UIView+Layer.m
//  HYNeiHan
//
//  Created by 上官惠阳 on 16/9/25.
//  Copyright © 2016年 上官惠阳. All rights reserved.
//

#import "UIView+Layer.h"

@implementation UIView (Layer)
- (void)setLayerCornerRadius:(CGFloat)cornerRadius
                 borderWidth:(CGFloat)borderWidth
                 borderColor:(UIColor *)borderColor {
    self.layer.cornerRadius = cornerRadius;
    self.layer.borderColor = borderColor.CGColor;
    self.layer.borderWidth = borderWidth;
    self.layer.masksToBounds = YES;
}

- (void)setLayerBorderColor:(UIColor *)layerBorderColor {
    self.layer.borderColor = layerBorderColor.CGColor;
    [self _config];
}

- (UIColor *)layerBorderColor {
    return [UIColor colorWithCGColor:self.layer.borderColor];
}

- (void)setLayerBorderWidth:(CGFloat)layerBorderWidth {
    self.layer.borderWidth = layerBorderWidth;
    [self _config];
}

- (CGFloat)layerBorderWidth {
    return self.layer.borderWidth;
}

- (void)setLayerCornerRadius:(CGFloat)layerCornerRadius {
    self.layer.cornerRadius = layerCornerRadius;
    [self _config];
}

- (void)layerRoundCorner
{
    [self _config];
    self.layerCornerRadius = self.frame.size.height / 2;
}
- (CGFloat)layerCornerRadius {
    return self.layer.cornerRadius;
}

- (void)_config {

    self.layer.masksToBounds = YES;
    // 栅格化 - 提高性能
    // 设置栅格化后，图层会被渲染成图片，并且缓存，再次使用时，不会重新渲染
    self.layer.rasterizationScale = [UIScreen mainScreen].scale;
    self.layer.shouldRasterize = YES;
}
//圆角制造
-(void)makeRoundedCorners
{
    self.layer.masksToBounds = YES;
    CGFloat r = self.frame.size.height / 2.0;

    self.layer.cornerRadius = r;
}
//边框制造
- (void)makeBorderColor:(UIColor *)color borderWidth:(CGFloat)width
{
    self.layer.borderColor = color.CGColor;
    self.layer.borderWidth = width;
}
//radius半径的圆角
-(void)makeRoundedCornersRadius:(CGFloat)radius
{
    self.layer.masksToBounds = YES;

    self.layer.cornerRadius = radius;
}
//给边框加阴影
- (void)addShadowColor:(UIColor *)color opacity:(CGFloat)opacity radius:(CGFloat)radius
{
    self.layer.shadowColor = color.CGColor;//阴影颜色

    self.layer.shadowOffset = CGSizeMake(4, 4);//偏移距离

    self.layer.shadowOpacity = opacity;//不透明度

    self.layer.cornerRadius = radius;//半径
}

//震动效果 amplitude为震动幅度
-(void)addShakeAnimationAmplitude:(CGFloat)amplitude
{
    CAKeyframeAnimation *shake = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    shake.values = @[[NSValue valueWithCATransform3D:CATransform3DMakeTranslation(-amplitude, -amplitude, -amplitude)], [NSValue valueWithCATransform3D:CATransform3DMakeTranslation(amplitude, amplitude, amplitude)]];
    shake.autoreverses = YES;
    shake.repeatCount = 2.0f;
    shake.duration = 0.07f;

    [self.layer addAnimation:shake forKey:nil];
}
@end
